<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use app\common\model\User;
use think\Exception;

class UserCouponGiveRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_user_coupon_give_record';

    protected $cacheModel = 'user.coupon_give_record';

    public $isGiveTxt = [
        0 => '、本券不可赠与好友使用。',
        1 => '、本券可赠与好友使用，优惠条件以及有效期不变。',
    ];

    /**
     * 用户优惠券赠送记录
     * @param $uid
     * @return array
     * @throws \Exception
     */
    public function getRecordList($uid)
    {
        $recordData = $this->getListPage($uid, 'id', [], 0, 'desc', 0);

        $data = [];
        foreach ($recordData['data'] as $item)
        {
            $data[] = [
                'cid' => $item['id'],
                'desc' => $item['cdesc'],
                'give_time' => $item['create_time'] ? date('Y.m.d H:i:s', $item['create_time']) : '',
                'type' => $item['uid'] == $uid ? 1 : 2
            ];
        }
        $recordData['data'] = $data;

        return $recordData;
    }

    /**
     * 获取赠送详情
     * @param int $uid
     * @param int $cid
     * @return array
     * @throws \Exception
     */
    public function getRecordDetail(int $uid, int $cid): array
    {
        $info = $this->getInfoById($cid);
        // 获取优惠券信息
        $cInfo = (new UserCoupon())->getInfoById($info['uid'] == $uid ? $info['uc_id'] : $info['new_uc_id']);
        // 获取使用规则信息
        $urData = (new CouponUseRange())->getInfoByIds($cInfo['ur_id']);

        $uid2 = $uid == $info['uid'] ? $info['give_uid'] : $info['uid'];

        $userinfo = (new User())->batchGetUserInfo([$uid2]);

        $isGive = $cInfo['is_give'] ?? 0;
        $urInfo = array_filter($urData[$cInfo['ur_id']] ?? []);
        $urInfo[] = (count($urInfo) + 1) . $this->isGiveTxt[$isGive];

        $data = [
            'cid' => (int)$cInfo['id'],
            'name' => $cInfo['name'] ?? '',
            'desc' => $cInfo['desc'] ?? '',
            'time_des' => $cInfo['end_time'] ? '有效期至' . date('Y.m.d', $cInfo['end_time']) : '',
            'dtype' => (int)($cInfo['type'] ?? 0),
            'discount' => $cInfo['discount'] ?? '0.00',
            'use_rule' => $urInfo,
            'nickname' => $userinfo[$uid2]['nickname'] ?? '匿名用户',
            'uen' => $userinfo[$uid2]['uen'] ?? '',
            'give_time' => $info['create_time'] ? date('Y.m.d H:i:s', $info['create_time']) : '',
            'type' => $info['uid'] == $uid ? 1 : 2,
        ];

        return $data;
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $where = [];
            if ($ids)
            {
                $where['id'] = ['in', $ids];
            } else
            {
                $where['uid|give_uid'] = $type[0];
            }

            $data = $this->where($where)->order("id {$sortType}")->column('uid,give_uid,cdesc,uc_id,new_uc_id,give_type,create_time', 'id');
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 未开启缓存从数据库获取分页数据
     * @param array $type
     * @param string $sortType
     * @return array|\think\Paginator
     */
    public function getPaginateInData($type, $sortType)
    {
        try
        {
            $where['uid|give_uid'] = $type[0];
            $data = $this->where($where)->order("id {$sortType}")
                ->field('id,uid,give_uid,cdesc,uc_id,new_uc_id,give_type,create_time')
                ->paginate()->toArray();
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}
